<script setup lang="ts">
import PictureUpload from "@/components/PictureUpload.vue";
import {onMounted, reactive, ref} from "vue";
import { message } from "ant-design-vue";
import {
  editPictureUsingPost, getPictureVoByIdUsingGet,
  listPictureTagCategoryUsingGet,
} from "@/api/pictureController.ts";
import {useRoute, useRouter} from "vue-router";

const router = useRouter();

const picture = ref<API.PictureVO>();
const pictureForm = reactive<API.PictureEditDTO>({});
const onSuccess = (newPicture: API.PictureVO) => {
  picture.value = newPicture;
  pictureForm.name = newPicture.name;
};

const doSubmit = async (values: any) => {
  const pictureId = picture.value?.id;
  if (!pictureId) {
    return;
  }

  const res = await editPictureUsingPost({
    id: pictureId,
    ...values,
  });
  if (res.code === 0 && res.data) {
    message.success("图片创建成功");
    await router.push({
      path: `/picture/${pictureId}`,
    });
  } else {
    message.error("创建失败，" + res.message);
  }
};

const categoryOptions = ref<string[]>([]);
const tagOptions = ref<string[]>([]);

const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet();
  if (res.code === 0 && res.data) {
    categoryOptions.value = (res.data.categoryList ?? []).map(data=>{
      return {
        value: data,
        label: data,
      };
    })
    tagOptions.value = (res.data.tagList ?? []).map(data=>{
      return {
        value: data,
        label: data,
      };
    })
  } else {
    message.error("获取标签和分类失败，" + res.message);
  }
};

const route = useRoute();
const getOldPicture = async () => {
  // 获取到 id
  const id = route.query?.id
  if (id) {
    const res = await getPictureVoByIdUsingGet({
      id,
    })
    if (res.code === 0 && res.data) {
      const data = res.data
      picture.value = data
      pictureForm.name = data.name
      pictureForm.introduction = data.introduction
      pictureForm.category = data.category
      pictureForm.tags = data.tags
    }
  }
}

onMounted(()=>{
  getTagCategoryOptions();
  getOldPicture();
})
</script>

<template>
  <div id="addPicturePage">
    <h2 style="margin-bottom: 16px">
      {{ route.query?.id ? '修改图片' : '创建图片' }}
    </h2>
    <PictureUpload :picture="picture" @success="onSuccess" />
    <a-form
        v-if="picture"
      :model="pictureForm"
      name="basic"
      @finish="doSubmit"
      layout="vertical"
    >
      <a-form-item label="名称" name="name">
        <a-input
          v-model:value="pictureForm.name"
          placeholder="请输入名称"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="简介" name="introduction">
        <a-textarea
          v-model:value="pictureForm.introduction"
          placeholder="请输入简介"
          :auto-size="{ minRows: 2, maxRows: 5 }"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="分类" name="category">
        <a-auto-complete
          v-model:value="pictureForm.category"
          placeholder="请输入分类"
          :options="categoryOptions"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="标签" name="tags">
        <a-select
          v-model:value="pictureForm.tags"
          mode="tags"
          placeholder="请输入标签"
          :options="tagOptions"
          allow-clear
        />
      </a-form-item>

      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">
          创建
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<style scoped>
#addPicturePage {
  max-width: 720px;
  margin: 0 auto;
}
</style>
